//
//  BassAnalysiseViewController.m
//  LiveSoftware
//
//  Created by lanou3g on 15/7/23.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "BassAnalysiseViewController.h"



#import "TitleForDetailMessageViewCell.h"

#import "FutureMatchViewCell.h"
#import "DetailBasketbModel.h"

#import "DetailMessageViewCell.h"



@interface BassAnalysiseViewController ()


//历史对战
@property(nonatomic,strong)NSMutableArray *historyVSModelArray;


//主队近期
@property(nonatomic,strong)NSMutableArray *hostRecentMatchModelArray;

//客队近期
@property(nonatomic,strong)NSMutableArray *visitRecentMatchModelArray;

//主队未来
@property(nonatomic,strong)NSMutableArray *hostFutureMatchModelArray;

//客队未来
@property(nonatomic,strong)NSMutableArray *visitFutureMatchModelArray;





@property(nonatomic,strong)UITableView *eventTableView;
//主队比分
@property(nonatomic,strong)NSMutableArray *hostScoresModelArray;

//客队比分
@property(nonatomic,strong)NSMutableArray *visitScoresModelArray;


//球队

//主队
@property(nonatomic,strong)NSMutableArray *hostRankModelArray;

//客队
@property(nonatomic,strong)NSMutableArray *visitRankModelArray;


@property(nonatomic,copy)NSString *hostIntro;
@property(nonatomic,copy)NSString *visitIntro;

//主队--客队名字
@property(nonatomic,copy)NSString *hostTeam;
@property(nonatomic,copy)NSString *visitTeam;




//主队--Vs
@property(nonatomic,copy)NSString *historyVsDescStr;

//主队--近期评价
@property(nonatomic,copy)NSString *hostRecentDescStr;
//客队--近期评价
@property(nonatomic,copy)NSString *visitRecentDescStr;
@end

@implementation BassAnalysiseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.tableView.scrollEnabled = NO;
    
    
    // 添加空白的 footView , 防止界面显示不全
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenIp6(5))];
    self.tableView.tableFooterView = footerView;
    
    
    //历史对战
    self.historyVSModelArray= [NSMutableArray array];
    
    //主队近期
    self.hostRecentMatchModelArray= [NSMutableArray array];
    
    //客队近期
    self.visitRecentMatchModelArray= [NSMutableArray array];
    
    //主队未来
    self.hostFutureMatchModelArray= [NSMutableArray array];
    
    //客队未来
    self.visitFutureMatchModelArray= [NSMutableArray array];
    
    
    //分析
    [self JionJieXiForSecondBasketball];
    
    

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}



#pragma mark  篮球解析
-(void)JionJieXiForSecondBasketball
{
    
    NSString *urlString = @"http://api.caipiao.163.com/jclq_live_matchDetail.html?mobileType=android&ver=4.8&channel=wap&apiVer=1.1&apiLevel=16";
    
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:self.visitId,@"visitId",self.hostId,@"hostId",@"2",@"type",self.matchId,@"matchId", nil];
    
    
    
    [LORequestManger  POST:urlString params:params success:^(id response)
     {
         NSDictionary *bigDict =( NSDictionary*)response;
         
         NSDictionary *smallDict = [bigDict objectForKey:@"data"];
         
         //主队
         self.hostTeam = smallDict[@"hostName"];
         self.visitTeam = smallDict[@"visitName"];
         
         
         
         //评价
         self.historyVsDescStr = [smallDict objectForKey:@"historyVsDesc"];
         self.hostRecentDescStr = [smallDict objectForKey:@"hostRecentDesc"];
         self.visitRecentDescStr = [smallDict objectForKey:@"visitRecentDesc"];
         
         
         
         
         NSArray *historyVSArray = [smallDict objectForKey:@"historyVS"];
         NSArray *hostRecentMatchArray = [smallDict objectForKey:@"hostRecentMatch"];
         NSArray *visitRecentMatchArray = [smallDict objectForKey:@"visitRecentMatch"];
         NSArray *hostFutureMatchArray = [smallDict objectForKey:@"hostFutureMatch"];
         NSArray *visitFutureMatchArray = [smallDict objectForKey:@"visitFutureMatch"];
         
         
         
         for (NSDictionary *dict in historyVSArray)
         {
             
             DetailBasketbModel *model = [DetailBasketbModel DetailBasketbModelWithDict:dict];
             [self.historyVSModelArray addObject:model];
             
         }
         for (NSDictionary *dict in hostRecentMatchArray)
         {
             
             DetailBasketbModel *model = [DetailBasketbModel DetailBasketbModelWithDict:dict];
             [self.hostRecentMatchModelArray addObject:model];
             
         }
         
         for (NSDictionary *dict in visitRecentMatchArray)
         {
             
             DetailBasketbModel *model = [DetailBasketbModel DetailBasketbModelWithDict:dict];
             [self.visitRecentMatchModelArray addObject:model];
             
         }
         for (NSDictionary *dict in hostFutureMatchArray)
         {
             
             DetailBasketbModel *model = [DetailBasketbModel DetailBasketbModelWithDict:dict];
             [self.hostFutureMatchModelArray addObject:model];
             
         }
         
         for (NSDictionary *dict in visitFutureMatchArray)
         {
             
             DetailBasketbModel *model = [DetailBasketbModel DetailBasketbModelWithDict:dict];
             [self.visitFutureMatchModelArray addObject:model];
             
         }
         
         [self.tableView reloadData];
         
         // 数据加载完毕, 刷新视图后, 发送一个通知, 将 contentSize 发送回去
         [[NSNotificationCenter defaultCenter] postNotificationName:@"basketballAnalysis" object:nil];
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"%@",error);
         NSLog(@"网络错误");
     }];
    
    
    
}








#pragma mark - 代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   
        if (indexPath.section ==0)
        {
            if (indexPath.row ==0)
            {
                return 70;
            }
            else
            {
                return 35;
            }
        }
        else if(indexPath.section ==1)
        {
            
            
            if (indexPath.row ==0)
            {
                return 70;
            }
            else if (indexPath.row == self.hostRecentMatchModelArray.count + 1)
            {
                return 70;
            }
            else
            {
                return 35;
            }
            
        }
        else
        {
            return 35;
        }
    
}


#pragma mark - 自定义分区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    SectionHeaderView *headerView = [[SectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenIp6(30))];
    headerView.backgroundColor = [UIColor blackColor];
    
    if (section ==0)
    {
        headerView.headerLabel.text = @"历史交锋";
    }
    else if(section ==1)
    {
        
        headerView.headerLabel.text = @"近期战绩";
        
    }
    else
    {
        headerView.headerLabel.text = @"未来赛事";
    }
    
    return headerView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return kScreenIp6(30);
    
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 3;
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
        
        if (section ==0)
        {
            return self.historyVSModelArray.count + 1;
            
        }
        else if(section ==1)
        {
            
            return self.hostRecentMatchModelArray.count + self.visitRecentMatchModelArray.count + 2;
            
            
        }
        else
        {
            return self.hostFutureMatchModelArray.count + self.visitFutureMatchModelArray.count  + 4;
        }
   
    
    
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   
        
        if(indexPath.section ==0)
        {
            if ( indexPath.row ==0) //历史对战
            {
                static NSString *cellIndetifier = @"titleCell";
                [tableView registerClass:[TitleForDetailMessageViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                TitleForDetailMessageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                cell.teamIntro.text = self.historyVsDescStr;
                
                return cell;
                
            }
            
            else
            {  static NSString *cellIndetifier = @"detailCell";
                [tableView registerClass:[DetailMessageViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                DetailMessageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                
                DetailBasketbModel*model = self.historyVSModelArray[indexPath.row -1];
                
                [cell setCellValueWithModel:model];
                return cell;
                
                
                
            }
            
            
        }
        else if(indexPath.section ==1) //近期
        {
            if ( indexPath.row ==0)  //row   >>0 //主队的近期评价
            {
                static NSString *cellIndetifier = @"titleCell";
                [tableView registerClass:[TitleForDetailMessageViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                TitleForDetailMessageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                cell.teamIntro .text = self.hostRecentDescStr;
                
                return cell;
                
                
            }
            
            else if(indexPath.row < self.hostRecentMatchModelArray.count +1 ) //row >>1,2,3,4
            {
                static NSString *cellIndetifier = @"detailCell";
                [tableView registerClass:[DetailMessageViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                DetailMessageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                
                DetailBasketbModel*model = self.hostRecentMatchModelArray[indexPath.row - 1];
                
                [cell setCellValueWithModel:model];
                
                return cell;
            }
            else if(indexPath.row == self.hostRecentMatchModelArray.count +1 )  //row 5,6,7,8  //客队近期评价
            {
                static NSString *cellIndetifier = @"titleCell";
                [tableView registerClass:[TitleForDetailMessageViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                TitleForDetailMessageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                cell.teamIntro.text = self.visitRecentDescStr;
                
                return cell;
                
            }
            
            else
                
            {  static NSString *cellIndetifier = @"detailCell";
                [tableView registerClass:[DetailMessageViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                DetailMessageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                
                DetailBasketbModel *model = self.visitRecentMatchModelArray[indexPath.row - self.hostRecentMatchModelArray.count -2  ];
                [cell setCellValueWithModel:model];
                
                return cell;
                
            }
        }
#pragma mark  未来
        else
        {
            if ( indexPath.row ==0)//标题
            {
                
                static NSString *cellIndetifier = @"futherCell1";
                
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier ];
                if(cell ==nil)
                {
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetifier];
                }
                
                
                //线
                UIView *footLineView = [[UIView  alloc]initWithFrame:CGRectMake( 0, 35, kScreenWidth, 0.5)];
                footLineView.backgroundColor = [UIColor colorWithWhite:0.699 alpha:1.000];
                [cell.contentView addSubview:footLineView];
                
                cell.selectionStyle = UITableViewCellSeparatorStyleNone;
                cell.textLabel.text = self.hostTeam;
                
                return cell;
                
            }
            else if (indexPath.row ==1)
            {
                static NSString *cellIndetifier = @"futherCell2";
                [tableView registerClass:[FutureMatchViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                FutureMatchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                [cell customTextColor];
                
                return cell;
                
            }
            
            else if(indexPath.row < self.hostFutureMatchModelArray.count + 2 )//主队
            {
                static NSString *cellIndetifier = @"futherCell3";
                [tableView registerClass:[FutureMatchViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                FutureMatchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                
                
                DetailBasketbModel*model = self.hostFutureMatchModelArray[indexPath.row - 2];
                
                [cell setCellValueWithModel:model];
                
                return cell;
            }
            else  if(indexPath.row == self.hostFutureMatchModelArray.count + 2 )//客队 --标题
            {
                
                static NSString *cellIndetifier = @"futherCell4";
                
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier ];
                if(cell ==nil)
                {
                    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetifier];
                }
                
                //线
                UIView *footLineView = [[UIView  alloc]initWithFrame:CGRectMake( 0, 35, kScreenWidth, 0.5)];
                
                footLineView.backgroundColor = [UIColor colorWithWhite:0.699 alpha:1.000];
                
                [cell.contentView addSubview:footLineView];
                
                cell.selectionStyle = UITableViewCellSeparatorStyleNone;
                
                cell.textLabel.text = self.visitTeam;
                
                return cell;
                
                
                
            }
            else if(indexPath.row == self.hostFutureMatchModelArray.count + 3)
            {
                
                
                static NSString *cellIndetifier = @"TitleFauther";
                [tableView registerClass:[FutureMatchViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                FutureMatchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                [cell customTextColor];
                
                return cell;
                
            }
            
            
            else
            {
                static NSString *cellIndetifier = @"FutherCell";
                [tableView registerClass:[FutureMatchViewCell class] forCellReuseIdentifier:cellIndetifier];
                
                FutureMatchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
                
                
                DetailBasketbModel *model = self.visitFutureMatchModelArray[indexPath.row - self.hostFutureMatchModelArray.count -4];
                [cell setCellValueWithModel:model];
                
                return cell;
                
            }
            
            
            
            
        }
        
    
    
    
    
}

@end
